package demo1;

import java.util.Arrays;

/**
 * --------神兽出没--------/
 *   ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * --------感觉萌萌哒--------/
 *
 * @author wuyoushan
 * @date 2018/5/3
 */
public class ClientTest {

    public static void main(String[] args) {
        try {
            ZookeeperOperator zkoperator = new ZookeeperOperator();
            zkoperator.connect("127.0.0.1:2181");

            byte[] data = new byte[]{'a', 'b', 'c', 'd'};

            if (null == zkoperator.existsNode("/root")) {
                zkoperator.create("/root", null);
                System.out.println("创建/root节点成功,数据为:");
            }
            System.out.println(Arrays.toString(zkoperator.getData("/root")));

            if (null == zkoperator.existsNode("/root/child1")) {
                zkoperator.create("/root/child1", data);
                System.out.println("创建//root/child1节点成功,数据为:");
            }
            System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));

            if (null == zkoperator.existsNode("/root/child2")) {
                zkoperator.create("/root/child2", data);
                System.out.println("创建//root/child2节点成功,数据为:");
            }
            System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));

            if (null == zkoperator.existsNode("/root/child3")) {
                String zktest = "ZooKeeper的Java API测试";
                zkoperator.create("/root/child3", zktest.getBytes());
            }
            System.out.println("创建//root/child3节点成功,数据为:");
            System.out.println("获取设置的信息：" + new String(zkoperator.getData("/root/child3")));

            System.out.println("节点孩子信息:");
            zkoperator.getChild("/root");

            zkoperator.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
